#include "MatchData.h"
#include "Team.h"
#include "People.h"
#include "ZStream.h"
#include "Utilities.h"

// MatchTeamData

MatchTeamData::MatchTeamData()
{
	Clear();
}

void MatchTeamData::Clear()
{
	m_teamID = Utilities::INVALID_ID;
	m_goals = 0;
	m_playerData.clear();
	m_trainerData.clear();
}

void MatchTeamData::Init(const Team* const i_team)
{
	Clear();
	if(i_team)
	{
		m_teamID = i_team->GetID();
		m_playerData.resize(Team::GetParams().TITOLARS_PLAYERS);
		for(size_t i=0; i<m_playerData.size(); ++i)
		{
			m_playerData[i].m_playerID = i_team->GetPlayersList()[i]->GetId();
			m_playerData[i].m_goals = 0;
			m_playerData[i].m_cards = CardNone;
			m_playerData[i].m_inj = 0;
			m_playerData[i].m_stamina = i_team->GetPlayersList()[i]->GetSkills().m_physicsSkills[Stamina];
		}

		m_trainerData.resize(Team::NUM_TRAINERS_ROLES);
		for(size_t i=0; i<m_trainerData.size(); ++i)
			m_trainerData[i].m_trainerID = (i_team->GetTrainersRole(i)) ? i_team->GetTrainersRole(i)->GetId() : i_team->GetTrainer(0)->GetId();
	}
}

void MatchTeamData::Serialize(ZStream& o_stream) const
{
	o_stream.WriteOpenTag("MatchTeamData");
	
	o_stream.WriteAttribute("teamID", m_teamID);
	o_stream.WriteAttribute("goals", m_goals);

	o_stream.WriteAttribute("numPlayers", m_playerData.size());
	for(size_t i=0; i<m_playerData.size(); ++i)
	{
		o_stream.WriteOpenTag("MatchPlayerData");
		o_stream.WriteAttribute("playerID", m_playerData[i].m_playerID);
		o_stream.WriteAttribute("cards", m_playerData[i].m_cards);
		o_stream.WriteAttribute("goals", m_playerData[i].m_goals);
		o_stream.WriteAttribute("inj", m_playerData[i].m_inj);
		o_stream.WriteAttribute("stamina", m_playerData[i].m_stamina);
		o_stream.WriteAttribute("evaluation", m_playerData[i].m_evaluation);
		o_stream.WriteCloseTag("MatchPlayerData");
	}

	o_stream.WriteAttribute("numTrainers", m_trainerData.size());
	for(size_t i=0; i<m_trainerData.size(); ++i)
	{
		o_stream.WriteOpenTag("MatchTrainerData");
		o_stream.WriteAttribute("trainerID", m_trainerData[i].m_trainerID);
		o_stream.WriteCloseTag("MatchTrainerData");
	}

	o_stream.WriteCloseTag("MatchTeamData");
}

void MatchTeamData::Deserialize(ZStream& i_stream)
{
	size_t temp;
	
	i_stream.SkipLine();
	i_stream.ReadAttribute(m_teamID);
	i_stream.ReadAttribute(m_goals);
	i_stream.ReadAttribute(temp);

	m_playerData.resize(temp);
	for(size_t i=0; i<temp; ++i)
	{
		i_stream.SkipLine();
		i_stream.ReadAttribute(m_playerData[i].m_playerID);
		i_stream.ReadAttribute(m_playerData[i].m_cards);
		i_stream.ReadAttribute(m_playerData[i].m_goals);
		i_stream.ReadAttribute(m_playerData[i].m_inj);
		i_stream.ReadAttribute(m_playerData[i].m_stamina);
		i_stream.ReadAttribute(m_playerData[i].m_evaluation);
		i_stream.SkipLine();
	}

	i_stream.ReadAttribute(temp);
	m_trainerData.resize(temp);
	for(size_t i=0; i<temp; ++i)
	{
		i_stream.SkipLine();
		i_stream.ReadAttribute(m_trainerData[i].m_trainerID);
		i_stream.SkipLine();
	}

	i_stream.SkipLine();
}

// MatchData

MatchData::MatchData()
{
	Clear();
}

void MatchData::Clear()
{
	m_playerMOM = 0;
	m_teamMOM = 0;
	m_events.clear();
	for(int j=0; j<2; ++j)
		m_teamData[j].Clear();
}

void MatchData::Init(const Team* const i_t1, const Team* const i_t2)
{
	Clear();
	m_teamData[0].Init(i_t1);
	m_teamData[1].Init(i_t2);
	m_events.reserve(100);
}

const MatchPlayerData* const MatchData::GetPlayerData(const int i_playerID) const
{
	for(size_t j=0; j<2; ++j)
		for(size_t i=0; i<m_teamData[j].m_playerData.size(); ++i)
			if(m_teamData[j].m_playerData[i].m_playerID == i_playerID)
				return &m_teamData[j].m_playerData[i];
	return 0;
}

void MatchData::Serialize(ZStream& o_stream) const
{
	o_stream.WriteOpenTag("MatchData");
	o_stream.WriteAttribute("TeamMoM", m_teamMOM);
	o_stream.WriteAttribute("PlayerMoM", m_playerMOM);
	for(int j=0; j<2; ++j)
		m_teamData[j].Serialize(o_stream);
	o_stream.WriteCloseTag("MatchData");
}

void MatchData::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();
	i_stream.ReadAttribute(m_teamMOM);
	i_stream.ReadAttribute(m_playerMOM);
	for(int j=0; j<2; ++j)
		m_teamData[j].Deserialize(i_stream);
	i_stream.SkipLine();
}
